/* ---------------------------------------------------------------------------------------------
 *
 *            Capital Alliance Software Confidential Proprietary
 *            (c) Copyright CAS 201{x}, All Rights Reserved
 *                          www.pekall.com
 *
 * ----------------------------------------------------------------------------------------------
 */
package com.elvis.music.view;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.SlidingDrawer;

import com.elvis.music.MusicApplication;

public class MySlidingDrawer extends SlidingDrawer {


    private float mLastMotionY;

    private int deltaY;

    private boolean isMoving = false;
    
    public MySlidingDrawer(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MySlidingDrawer(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        if (!isMoving) {
            super.dispatchDraw(canvas);
        }
        final long drawingTime = getDrawingTime();
        final View handle = this.getHandle();
        
        drawChild(canvas, handle, drawingTime);
        
        View mContent = super.getContent();
        canvas.save();
        canvas.translate(0, handle.getTop());
        drawChild(canvas, mContent, drawingTime);
        canvas.restore();

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        final int action = event.getAction();
        final float y = event.getY();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                mLastMotionY = y;
                break;
            case MotionEvent.ACTION_MOVE:
                isMoving = true;
                deltaY = (int) (mLastMotionY - y);
                if(deltaY>0&& this.isOpened())
                    break;
                if(deltaY<0&&(!this.isOpened()))
                    break;
                mLastMotionY = y;
                MusicApplication.getInstance().setLrcCenter(
                        MusicApplication.getInstance().getLrcCenter() + deltaY / 2);
                break;
            case MotionEvent.ACTION_UP:
                break;
            case MotionEvent.ACTION_CANCEL:
                break;

        }

        return super.onTouchEvent(event);
    }
}
